package com.deer.wms.ASN.manage.web;

import com.deer.wms.ASN.manage.model.buy.*;
import com.deer.wms.ASN.manage.model.file.FileData;
import com.deer.wms.ASN.manage.service.RequestBuyDetailService;
import com.deer.wms.ASN.manage.service.RequestBuyService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.review.manage.model.BillRecord;
import com.deer.wms.review.manage.service.BillRecordService;
import com.deer.wms.review.manage.service.CaseMasterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2020/01/02.
*/
@Api(description = "采购信息api接口")
@RestController
@RequestMapping("/request/buys")
public class RequestBuyController {

    @Autowired
    private RequestBuyService requestBuyService;

    @Autowired
    private CaseMasterService caseMasterService;

    @Autowired
    private RequestBuyDetailService requestBuyDetailService;

    @Autowired
    private BillRecordService billRecordService;

    @ApiOperation(value = "新增采购信息", notes = "新增采购信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody InsertData insertData,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        requestBuyService.createRequestBuy(insertData,currentUser);
        return ResultGenerator.genSuccessResult();
    }



    @ApiOperation(value = "审核", notes = "审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/review")
    public Result review(String billNo, Integer reviewResult,Integer caseDetailId,String memo, @ApiIgnore @User CurrentUser currentUser) {
        RequestBuy requestBuy = requestBuyService.findBy("bill_no", billNo);
        //-1不通过   0-流转中  1-通过  2-初始化
        Integer lastReviewResult = caseMasterService.review(caseDetailId,currentUser.getUserId(),reviewResult,memo);

        if (lastReviewResult == -1) {
            requestBuy.setState(-1);
            requestBuyService.updateById(requestBuy);
            BillRecord billRecord = billRecordService.findByBillNoAndType(billNo,9);//采购审核
            billRecord.setResult("退回");
            billRecord.setModifyTime(DateUtils.getNowDateTimeString());
            billRecord.setModifyUserId(currentUser.getUserId());
            billRecord.setModifyUserName(currentUser.getUserName());
            billRecordService.updateById(billRecord);
        } else if (lastReviewResult == 0) {
            requestBuy.setState(0);
            requestBuyService.updateById(requestBuy);
            BillRecord billRecord = billRecordService.findByBillNoAndType(billNo,9);//采购审核
            billRecord.setResult("流转");
            billRecordService.updateById(billRecord);
        }else if (lastReviewResult == 2) {
            requestBuy.setState(2);
            requestBuyService.updateById(requestBuy);
        } else if (lastReviewResult == 1) {
            requestBuy.setState(1);
            requestBuyService.updateById(requestBuy);
            BillRecord billRecord = billRecordService.findByBillNoAndType(billNo,9);//采购审核
            billRecord.setResult("通过");
            billRecord.setModifyTime(DateUtils.getNowDateTimeString());
            billRecord.setModifyUserId(currentUser.getUserId());
            billRecord.setModifyUserName(currentUser.getUserName());
            billRecordService.updateById(billRecord);
        }
        return ResultGenerator.genSuccessResult();
    }


    //motify-luowei 0701 未发现使用的地方
    @ApiOperation(value = "重新发起审核", notes = "重新发起审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Deprecated
    @GetMapping("/reReview")
    public Result reReview(String billNo, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        RequestBuy requestBuy = requestBuyService.findBy("bill_no", billNo);

        //初始化状态 2
        requestBuy.setState(2);
        requestBuyService.updateById(requestBuy);
        caseMasterService.createCase(billNo, 1, currentUser.getUserId(), requestBuy.getWareId(),requestBuy.getOrganizationId());

        return ResultGenerator.genSuccessResult();
    }



    @GetMapping("/delete")
    public Result delete(Integer id) {
        requestBuyService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "增删master、detail", notes = "增删master、detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/update")
    public Result update(@RequestBody InsertData insertData, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        BillRecord billRecord =new BillRecord();
        billRecord.setBillNo(insertData.getRequestBuy().getBillNo());
        billRecord.setRecordType(14);//采购修改
        billRecord.setCreateTime(DateUtils.getNowDateTimeString());
        billRecord.setCreateUserId(currentUser.getUserId());
        billRecord.setCreateUserName(currentUser.getUserName());
        billRecordService.save(billRecord);
        RequestBuy requestBuy = insertData.getRequestBuy();
        requestBuyService.updateById(requestBuy);

        List<RequestBuyDetail> requestBuyDetails = insertData.getRequestBuyDetails();
//        requestBuyDetailService.deleteByBillNo(insertData.getRequestBuy().getBillNo());

        for(RequestBuyDetail requestBuyDetail:requestBuyDetails){
//            String detailNo = insertData.getRequestBuy().getBillNo() + "-" + (i+1);
//            requestBuyDetail.setDetailNo(detailNo);
//            requestBuyDetail.setBillNo(insertData.getRequestBuy().getBillNo());

            if(insertData.getInsertType() ==-2){
                requestBuyDetail.setState(2);
            }else if(insertData.getInsertType() ==0){
                requestBuyDetail.setState(0);
            }
            requestBuyDetailService.updateById(requestBuyDetail);
        }

        BillRecord billRecord1 =billRecordService.findByBillNoAndType(insertData.getRequestBuy().getBillNo(),14);//采购修改
        billRecord1.setModifyTime(DateUtils.getNowDateTimeString());
        billRecord1.setModifyUserId(currentUser.getUserId());
        billRecord1.setModifyUserName(currentUser.getUserName());
        billRecordService.updateById(billRecord1);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        RequestBuy requestBuy = requestBuyService.getById(id);
        return ResultGenerator.genSuccessResult(requestBuy);
    }


    @ApiOperation(value = "采购信息", notes = "采购信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/list")
    public Result list(RequestBuyCriteria criteria,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<RequestBuyDto> list = requestBuyService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "归档采购信息 传billNo", notes = "归档采购信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/file")
    public Result file(@RequestBody FileData fileData, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        for(int i=0;i<fileData.getBillNos().size();i++){
            String billNo = fileData.getBillNos().get(i);
            requestBuyService.createFile(billNo,currentUser);
        }

        return ResultGenerator.genSuccessResult();
    }
}
